<?php

class Application_Model_SegmentoAgenda extends Zend_Db_Table_Abstract {

    protected $_name = 'segmento_agenda';
    protected $_primary = 'codigo';
    private static $instance;

    public static function getInstance() {
        if (!isset(self::$instance)) {
            self::$instance = new Application_Model_SegmentoAgenda();
            return self::$instance;
        }
        return self::$instance;
    }

    public function insert(array $data, $segmento) {
        foreach ($data as $row){
            $insert = array ('hora'=>$row['hora'], 
                            'dt_cadastro' => new Zend_Db_Expr('NOW()'), 
                            'segmento'=>$segmento, 
                            'ordem'=>$row['ordem']
                            );
                    parent::insert($insert);
        }
    }
    
    public function update(array $data, $segmento, $codigos, $where = ''){
        $codigosNovos = array();
        if (sizeof($codigos) == 0) $codigos = array();
        foreach ($data as $row){
            if (!is_null($row['codigo'])){
                $where = 'codigo = ' . $row['codigo'];
                parent::update($row, $where); 
            }else{
                $insert = array ('hora'=>$row['hora'], 
                                 'dt_cadastro' => new Zend_Db_Expr('NOW()'), 
                                 'segmento'=>$segmento, 
                                 'ordem'=>$row['ordem'] 
                                );
                parent::insert($insert);
                $codigosNovos[] = $this->getAdapter()->lastInsertId();
            }
        }

        $merge = array_merge($codigosNovos, $codigos);
        $codManter = implode(",", $merge);

        if(sizeof($codigosNovos)>0 || sizeof($codigos)>0){
            $where2 = 'codigo not in ('. $codManter .') and dt_exclusao is null and segmento = ' . $segmento;
        }else{
            $where2 = 'dt_exclusao is null and segmento = \''. $segmento . '\'';
        }

        $parceria['dt_exclusao'] = Zend_Date::now()->get('Y/M/d');
        parent::update($parceria, $where2);
    }

    public function getById($id) {
        return $this->fetchRow("codigo = " . $id ." and dt_exclusao is null");
    }
    
    public function getBySegmento($segmento) {
        $select = $this->getAdapter()->select();
        $select->from(
                    array('sa' => $this->_name),
                    array( 'sa.codigo', 'sa.ordem','sa.hora'
                    ),
                    $this->_schema
                )
                ->where('sa.segmento = \''.$segmento.'\' and sa.dt_exclusao is null')
                ->order('sa.ordem');
        
        return $this->getAdapter()->fetchAll($select);
    }

    public function lista($page, $rp, $sortname, $sortOrder,$data) {
        $sql = 'select codigo,hora,dt_cadastro
                from segmento
                where dt_exclusao is null ';

        if (isset($data['hora']) && !empty($data['hora'])) {
            $sql.=' and hora like \'' . $data['hora'] . '%\'';
        }

        if (isset($data['dt_cadastro']) && !empty($data['dt_cadastro'])) {
            $sql.=' and dt_cadastro = \'' . $data['dt_cadastro'] . '\'';
        }

        if (isset($sortOrder) && isset($sortname)) {
            $sql .= ' order by ' . $sortname . ' ' . $sortOrder;
        }

        $rs = $this->getAdapter()->query($sql)->fetchAll();
        $paginator = Zend_Paginator::factory($rs);
        $paginator->setCurrentPageNumber($page);
        $paginator->setItemCountPerPage($rp);

        return $paginator;
    }

    public function remove($id, $segmento){
        $parceria = $this->getById($id)->toArray();
        $where = ' codigo = ' .$id.' and segmento = '.$segmento;
        $parceria['dt_exclusao'] = Zend_Date::now()->get('Y/M/d');
        parent::update($parceria, $where);
    }
    
    public function getList(){
        $sql = 'select codigo, descricao from segmento where dt_exclusao is null';
        return $this->getAdapter()->query($sql)->fetchAll();
    }
    
    public function getHoraBySegmento($hora,$segmento){
        $sql = 'select hora from segmento_agenda where hora = \''.$hora.'\' and segmento = '.$segmento;
        return $this->getAdapter()->query($sql)->fetchAll();
    }
    
    public function getHorasBySegmento($segmento){
        $sql = 'select codigo,hora from segmento_agenda where segmento = '.$segmento;
        return $this->getAdapter()->query($sql)->fetchAll();
    }
    
}